<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Profile extends MX_Controller
{

    public function __construct()
    {
        parent::__construct();

        $this->template->set_theme('admin');
        $this->template->set_layout('default');
        $this->template->set_partial('side_menu', 'layouts/side_menu');
        $this->template->set_partial('nav_bar', 'layouts/nav_bar');
        $this->load->model('admin_member_model');
    }

    function index()
    {

        $data['profile']['details'] = $this->admin_member_model->getMemberdetails();


        $this->template->build('member/profile', $data);
    }

    function update_profile()
    {
        if ($this->input->post('upload')) {
            //upload Photo and thumbnail Starts
            //Form -- TRUE
            // Banner Upload Configuration Start
            $filevariable = array_keys($_FILES);
            $userfile = $filevariable[0];
            $image = $_FILES[$userfile];

            //$vSize = $this->input->post('banner_size');
            //$vSize = explode('X',$vSize);
            //$vH=0;
            //$vW=0;

            //$vW=$vSize[0];
            //$vH=$vSize[1];


            $imageData = array(
                'userfile' => $userfile,
                'imagearray' => $image,
                'imagepath' => './assets/images/',
                'allowedtype' => 'jpg|png|gif|jpeg',
                'overwrite' => 'true',
                'removespace' => 'true',
                'customefilename' => '', //do not use extension if not need leave blank
                'namewithtime' => 'yes', //time(image_121326.jpg)at last of file name if not need leave blank
                'separator' => '-', //format - image_121326.jpg (.)not allowed, if not require leave blank
                'prefix' => 'admin_', //format my_image_121326.jpg , if not require leave blank
                'errmessage' => '', // If blank default message will shown
                'sucessmessage' => '', // If blank default message will shown
                'createthumb' => 'no',
                'thumbwidth' => '',
                'thumbheight' => '',
                'thumbpath' => '' // Dont write thumb folder name in path

            );
            //Banner upload Configure End
            $uploadData = $this->commoncall->upload($imageData);


            if ($uploadData == TRUE) {


                $type = $this->input->post('type');

                $profiledata = array(


                    'image' => $uploadData['data'],
                    'type' => $type,
                    'name' => $this->input->post('admin_name'),
                );

                //--Delete Old Image File--//


                $sql = "Select image from member where type = $type";
                $filename = $this->db->query($sql)->row()->image;
                $f = BASEPATH . '../assets/images/' . $filename;
                unlink($f);


                $this->db->where('type', $this->input->post('type'));
                $this->db->update('member', $profiledata);
                $this->session->set_flashdata('message', 'Profile Uploded Sucessfully!... ');
                redirect('secure/member/profile/');

            } else {
                $profiledata = array(
                    //'file'=>$uploadData['data'],
                    'name' => $this->input->post('admin_name'),
                );
            }

            $this->db->where('type', $this->input->post('type'));
            $this->db->update('member', $profiledata);
            $this->session->set_flashdata('message', 'Profile Update Sucessfully!... ');
            redirect('secure/member/profile/');
        } else {
            $this->session->set_flashdata('message', 'fields can not be null!... ');
            redirect('secure/member/profile/');

        }


    }

}
